package com.bigshark.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigshark.api.annotation.Login;
import com.bigshark.api.annotation.LoginUser;
import com.bigshark.api.form.CVPhotoForm;
import com.bigshark.api.model.CVPhoto;
import com.bigshark.api.model.Conventions;
import com.bigshark.api.model.User;
import com.bigshark.api.service.CVPhotoService;
import com.bigshark.api.service.ConventionsService;
import com.bigshark.common.utills.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/api/photo")
public class CVPhotoController {
    @Autowired
    private CVPhotoService cvPhotoService;
    @Autowired
    private ConventionsService conventionsService;

    @Login
    @PostMapping("/")
    @ApiOperation(value = "新增会展图片集",notes = "新增会展图片集",tags = "会展返图接口",httpMethod = "POST")
    public R createPhoto(@RequestBody CVPhotoForm form, @ApiIgnore @LoginUser User user){
        Conventions cv= conventionsService.getById(form.getId());
        if (null == cv){
            return R.error("ID不存在");
        }
        if(user.getId()!=cv.getUserId()){
            return R.error("没有此权限");
        }
        CVPhoto cvPhoto = new CVPhoto();
        cvPhoto.setCvId(form.getId());
        cvPhoto.setImgIntroduction(form.getInfo());
        cvPhoto.setImgTitle(form.getTitle());
        cvPhoto.setImgUrl(form.getUrls());
        cvPhoto.setStatue(0);
        if(!cvPhotoService.save(cvPhoto)){
            return R.error("添加失败");
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "会展图片集",notes = "会展图片集",tags = "会展返图接口",httpMethod = "GET")
    public R queryPhotos(@PathVariable("id")Long id){
        List<CVPhoto> data =  cvPhotoService.list(new QueryWrapper<CVPhoto>().eq("cv_id",id));
        return R.ok().put("data",data);
    }

}
